//
//  CMDBDynamicManager.swift
//  CMDB
//
//  Created by 王磊 on 17/4/7.
//  Copyright © 2017年 CMDB. All rights reserved.
//

import UIKit

class CMDBDynamicManager: NSObject {
    
    static let getInstance: CMDBDynamicManager = CMDBDynamicManager()
    
    private override init() {
        super.init()
        
    }
}

extension CMDBDynamicManager: CMDBDynamicManagerDelegate {}

extension CMDBDynamicManager {
    
    func queryHomeDynamicList(dynamicListParam: CMDBHomeDynamicParam,tableView: UITableView,handler: CMDBDynamicListHandler,error: CMDBHttpErrorHandler,failure: CMDBHttpFailureHandler) {
        
        CMDBHttpManager.getInstance.request(dynamicListParam.URLString(), param: dynamicListParam.keyValues, ownPage: dynamicListParam.otype, tableView: tableView, success: { [weak self] (sysTime, data, page) in
            
            let home = CMDBHomeDynamicBean.objectWithKeyValues(data as! NSDictionary) as! CMDBHomeDynamicBean
            
            handler(homeDynamic: home)
            
            //            self!.saveDynamicHome(home, identifier: "CMDBHomeDynamic_\(CMDBApplicationManager.getInstance.account!.uid)")
            
            }, error: error, failure: failure)
    }
}
extension CMDBDynamicManager {
    
    func saveDynamicHome(homeBean: CMDBHomeDynamicBean, identifier: String) {
        
        CMDBCacheDataManager.getInstance.saveDictionary(homeBean.keyValues, identifier: identifier)
    }
    
    func queryDynamicHomeCache(identifier: String) throws -> CMDBHomeDynamicBean {
        
        guard let cache = CMDBCacheDataManager.getInstance.queryData("CMDBHomeDynamic_\(identifier)") else {
            
            printLog(CMDBHomeControllerExceptionCodeType.DynamicListEmpty.rawValue)
            
            throw CMDBHomeControllerExceptionType.ExceptionCode(code: CMDBHomeControllerExceptionCodeType.DynamicListEmpty)
        }
        
        return CMDBHomeDynamicBean.objectWithKeyValues(cache) as! CMDBHomeDynamicBean
    }
}

// MARK: 发布动态
/*
 let imageParam = CMDBImageParam()
 
 imageParam.image = UIImage(named: "密码")
 
 imageParam.url = "123123123"
 
 let pub = CMDBPubDynamicParam()
 
 pub.images += [imageParam]
 
 pub.content = "123123"
 
 printLog(pub.keyValues)
 
 CMDBDynamicManager.getInstance.pubDynamic(pub, handler: { (dynamic) in
 
 }, error: { (rescode) in
 
 }) {
 
 }
 */
//

extension CMDBDynamicManager {
    
    func pubDynamic(pubDynamicParam: CMDBPubDynamicParam, handler: CMDBPubDynamicHandler, error: CMDBHttpErrorHandler, failure: CMDBHttpFailureHandler) throws {
        
        guard !pubDynamicParam.content.isEmpty else {
            
            printLog(CMDBHomeControllerExceptionCodeType.PubDynamicContentEmpty.rawValue)
            
            throw CMDBHomeControllerExceptionType.ExceptionCode(code: CMDBHomeControllerExceptionCodeType.PubDynamicContentEmpty)
        }
        
        CMDBHttpManager.getInstance.request(pubDynamicParam.URLString(), param: ["content":pubDynamicParam.content,"imgs":pubDynamicParam.imgs], success: { (sysTime, data, page) in
            
            let dynamic = CMDBDynamicBean.objectWithKeyValues(data as! NSDictionary) as! CMDBDynamicBean
            
            handler(dynamic: dynamic)
            
            }, error: error, failure: failure)
    }
}
// MARK:  动态详情
extension CMDBDynamicManager {
    
    func queryDynamicDetail(dynamicDetailParam: CMDBDynamicDetailParam, handler: CMDBDynamicDetailHandler, error: CMDBHttpErrorHandler, failure: CMDBHttpFailureHandler) {
        
        CMDBHttpManager.getInstance.request(dynamicDetailParam.URLString(), param: dynamicDetailParam.keyValues, success: { (sysTime, data, page) in
            
            let dynamic = CMDBDynamicBean.objectWithKeyValues(data as! NSDictionary) as! CMDBDynamicBean
            
            handler(dynamic: dynamic)
            
            }, error: error, failure: failure)
        
    }
}